spring cloud(1) ---- 服务治理 Eureka

一.搭建服务注册中心:

maven:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

    </dependencies>

main启动类:

@SpringBootApplication
@EnableEurekaServer
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}

1.默认情况下,服务注册中心会尝试把自己也注册成一个服务,所以我们在配置文件中禁止把自己注册成服务

server.port=1111

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 register-with-eureka: false(不向注册中心中注册自己)
 fetch-registry: false(不进行服务检索,只进行服务维护)

2.启动main类,访问 http://localhost:1111 即可看到服务注册中心界面

二.高可用服务注册中心

Eureka Server 的设计一开始就考虑了高可用问题,在Eureka的服务治理设计中,所有节点及时服务提供方,也是服务消费方,服务注册中心也不例外。Eureka Server的高可用实际上就是贱自己作为服务想其他服务注册中心注册自己,这样就形成一组互相注册的服务注册中心,以实现服务清单的互相同步,打倒高可用的效果。

三.搭建服务提供者

1.maven:

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2.main启动类:

@SpringBootApplication
@EnableEurekaClient
public class ServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServerApplication.class, args);
	}

}

*在主类中通过加上@EnableEurekaClient注解,激活Eureka中的DiscoveryClient实现(自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例),才能实现上述Controller中对服务信息的输出

3.配置文件

server:
  port: 1114

spring:
  application:
    name: service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/

*通过eureka.application.name指定服务名

4.Controller类

@RestController
public class HelloController {
    private final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
}

启动服务即可在服务注册中心中查看到有一个服务名为SERVICE-HI的服务

四.服务消费者

在spring cloud中 服务发现石油eureka客户端万恒,而服务消费的任务是有ribbon完成

1.maven:

<description>Demo project for Spring Boot</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>

2.main启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class ConmuserMain {

    @Bean
    @LoadBalanced
    public RestOperations restTemplate(RestTemplateBuilder builder){
        return  builder.build();
    };
    public static void main(String[] args) {
        SpringApplication.run(ConmuserMain.class,args);
    }
}

*通过@EnableDiscoveryClient注解让该应用注册为Eureka客户端应用,以获得服务发现的能力,同事,在该主类中创建RestTemplate的Spring Bean实例,并通过@LoadBalanced注解开启客户端负载均衡(如果不加入该注解,无法通过服务名访问服务)

3.配置文件

spring:
  application:
    name: ribbon-consumer
server:
  port: 1113
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/

4.controller、service类
 

@RestController
public class ConsumerController {

    @Autowired
    ConsumerService consumerService;
    @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
    public String helloConsumer(){
        try {
            return consumerService.getForEntity();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ee";
    }
}

@Service
public class ConsumerService {
    @Autowired
    RestOperations restTemplate;

    public String getForEntity() throws Exception {  
        return restTemplate.getForEntity("http://SERVICE-HI/hi",String.class).getBody();
    }

}

最后访问http://localhost:1113/hi即可通过服务消费者访问service-hi服务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值