spring boot 通过Eureka搭建微服务
一、构建一个micro-parent01项目,下面创建3个quickstart二、micro-registry 注册中心搭建1、application.yml 全局配置server:port:8080eureka:instance:hostname: localhostclient: #not a client,don't regis
·
一、构建一个micro-parent01项目,下面创建3个quickstart
ps:micro-parent01的pom.xml文件在最后面
二、micro-registry 注册中心搭建
1、application.yml 全局配置
server:
port:8080
eureka:
instance:
hostname: localhost
client: #not a client,don't register with yourself
registerWithEureka: false
fetchRegistry: false
注意:
client: #not a client,don't register with yourself registerWithEureka: false fetchRegistry: false
如果不加上的话会报错:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
而且 registerWithEureka和fetchRegistry的冒号后面也要空一格空格才不会出错,就是该两个单词需要改变成关键字的颜色才不会出错。
2、编写启动程序
@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryApplication.class);
}
}
3、main方法启动,通过网页:localhost:8080启动查看
三、向注册中心注册(提供)服务
1、配置application.yml文件(该文件实在resources下面)
# HTTP (Tomcat) port
server:
port: 8081
#Application name
spring:
application:
name: micro-service
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
2、基于RESTful风格编写一个服务并暴露
@RestController
public class HelloWorldController {
@RequestMapping("hello/{name}")
public String hello(@PathVariable String name) {
return name + " say hello";
}
}
3、编写启动程序
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
4、启动成功
5、网页刷新http://localhost:8080
此时我们就会发现服务已经注册到了注册中心
四 micro-client端发现(调用)服务
1、配置文件application.yml
# HTTP (Tomcat) port
server:
port: 8082
#Application name
spring:
application:
name: micro-client
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
# application parameters
app:
service-url: http://MICRO-SERVICE/
@RestController
public class CallHelloController {
@Autowired
private CallHelloService callHelloService;
@GetMapping("hello")
public String hello(String name) {
String result = callHelloService.callHello(name);
return result;
}
}
3、编写service层,通过service来调用服务
@Service
public class CallHelloService {
@Value("${app.service-url}")
private String appServiceUrl;
@Autowired
private RestTemplate restTemplate;
public String callHello(String name) {
// 是一个http client
ResponseEntity
result = restTemplate.postForEntity(appServiceUrl + "hello/" + name, null, String.class);
return result.getBody();
}
}
注意: RestTemplate是spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http的方法,能够大大提高客户的编写效率。调用RestTemplate的默认构造函数,RestTemplate对象在底层通过java.net包下面的实现创建HTTP请求,可以通过ClientHttpRequestFactory指定不同的Http请求方式。
ClientHttpRequestFactory接口主要提供了两种实现方式:
一种是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接。
另一种方式是使用HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务,使用HttpClient可以配置连接池和证书等信息。
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#rest-resttemplate
一种是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接。
另一种方式是使用HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务,使用HttpClient可以配置连接池和证书等信息。
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#rest-resttemplate
@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@LoadBalanced 注解开启均衡负载能力
5、网页启动成功截图
6、通过localhost:8082/hello?name=ycx能得到下面的,就证明已经搭建成功了
micro-parent01的pom.xml文件
4.0.0
com.usst
micro-parent01
1.0-SNAPSHOT
micro-client
micro-registry
micro-service
pom
website
scp://webhost.company.com/www/website
UTF-8
UTF-8
org.springframework.cloud
spring-cloud-starter-parent
Camden.SR6
org.springframework.cloud
spring-cloud-starter
org.springframework.cloud
spring-cloud-starter-eureka-server
junit
junit
test
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.7
1.7
UTF-8
${java.home}/lib/rt.jar
更多推荐
已为社区贡献1条内容
所有评论(0)