标签:disco group 目录 enabled interface toc inter 服务框架 hostname
目录
springCloud是一个微服务框架集。
eureka来实现zookeeper;
选择版本: 1.5.17
cloud dicovery --- eureka server
# 启动类添加注解:
@EnableEurekaServer
# application.yml
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
# application.yml
spring:
application:
name: server中心
profiles:
active: server1
# application-server1.yml
server:
port: 8761
eureka:
instance:
hostname: server1
client:
service-url:
defaultZone: http://127.0.0.1:8762/eureka/ # 后面可以添加更多地址,用逗号分隔
# application-server2.yml
server:
port: 8762
eureka:
instance:
hostname: server2
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
启动参数
--spring.profiles.active=prod
和注册中心一样
和注册中心一样
spring:
application:
name: service01
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
和之前一样
# 添加注解
@EnableDiscoveryClient
# 添加属性
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
spring:
application:
name: controller01
server:
port: 8764
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("test")
public String test() {
String forObject = restTemplate.getForObject("http://service01/test", String.class);
return forObject;
}
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
@EnableFeignClients
@EnableDiscoveryClient
@FeignClient(value = "springcloud-server")
public interface TestService {
@RequestMapping("/test")
public String test();
}
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(){
return testService.test();
}
}
标签:disco group 目录 enabled interface toc inter 服务框架 hostname
原文地址:https://www.cnblogs.com/birdofparadise/p/10013165.html