Springcloud服务注册与发现

Springcloud服务注册与发现

1.服务注册与发现的相关概念

在一个web项目运行中,会产生很多请求,每种请求理解为一种服务,那么就会有很多服务,这些服务可以理解为服务消费者。既然有消费者当然也有提供者,这些处理请求的容器可以理解为服务消费提供者。有了消费者和提供者那么肯定有一个管理它们的地点,这个地点就是消息中心,当一个服务消费提供者生成时,它就会去消息中心自动注册,消息中心就会给它一个名字(同种服务,同种名字,名字必须大写否则负载均衡不起作用,还不能有“_”),这一过程可以理解为服务注册,同时它还会发送“心跳”给消息中心,这一动作可以保证服务消费提供者的健康。而服务消费者也需要去注册,不同得是服务消费者是带着消费提供者的名字去的,消息中心通过名字去负载均衡调度最合适的容器给它,这一过程可以理解为服务发现。

2.消息中心的搭建

maven依赖:
            <!-- 继承springboot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <!-- 导入springcloud服务端 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!-- 导入eureka服务端依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>
Java代码:
        package cn.zyp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;


@SpringBootApplication
@EnableEurekaServer
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

Application.properties配置文件:
server.port=8761
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

配置完后启动,访问localhost:8761即可出现管理页面

3. 服务注册(以邮箱发送服务为例)

Maven 依赖:
<!--继承springboot-->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

<!-- 导入springcloud服务端 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
            <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!-- 导入eureka客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
启动类代码:
    package cn.zyp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}
Controller层代码:
package cn.zyp;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class MainController {
    @Autowired
    private RestTemplate rtl;


    @PostMapping("/mailsend")
    public String sendmail(String to,String subject,String text){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("to", to);
        map.put("subject", subject);
        map.put("text", text);
        //将map集合保存为请求体
        HttpHeaders hh =new HttpHeaders();
        hh.setContentType(MediaType.APPLICATION_JSON);
        hh.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<Map<String,Object>> request = new HttpEntity<Map<String,Object>>(map, hh);
        //调用email服务
        String controller ="/send";
        //通过注册中心客户端负载均衡 获取一台主机来调用
        String res=rtl.postForObject("http://EMAIL/"+controller,request,String.class);
        return res;
    }
}

application.properties:
spring.mail.default-encoding=UTF-8
spring.mail.host= smtp.163.com
spring.mail.password=你的授权码
spring.mail.port= 25
spring.mail.username=当前用户账号
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=注册名
server.port=端口

4.服务发现(以邮箱发送服务为例)

    Maven依赖:
<!--继承springboot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <!-- 导入springcloud服务端 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- ribbo依赖jar包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <!-- 导入eureka客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
启动类:
    package cn.zyp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**  
 * @EnableDiscoveryClient 将当前服务注册到Eureka服务器  
 * @EnableEurekaClient继承@EnableDiscoveryClient 作用相同不要纠结  
 *有 @EnableEurekaClient可以不用@EnableDiscoveryClient
 *有@SpringBootApplication可以不用@Configuration
 */  
@SpringBootApplication
@EnableEurekaClient
/**
 * 表示当前服务需要的服务的名称
 *
 */
@RibbonClient(value="email")
public class Main {
    //启动负载均衡
    @LoadBalanced    
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

Controller层主要代码:
package cn.zyp;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class MainController {
    @Autowired
    private RestTemplate rtl;


    @PostMapping("/mailsend")
    public String sendmail(String to,String subject,String text){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("to", to);
        map.put("subject", subject);
        map.put("text", text);
        //将map集合保存为请求体
        HttpHeaders hh =new HttpHeaders();
        hh.setContentType(MediaType.APPLICATION_JSON);
        hh.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<Map<String,Object>> request = new HttpEntity<Map<String,Object>>(map, hh);
        //调用email服务
        String controller ="/send";
        //通过注册中心客户端负载均衡 获取一台主机来调用
        String res=rtl.postForObject("http://EMAIL/"+controller,request,String.class);
        return res;
    }
}
application.properties:
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=服务名
server.port=端口

5.fegin服务发现(以邮箱发送服务为例)

Mave依赖:
<!-- 继承springboot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <!-- 导入springcloud服务端 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- fegin依赖jar包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!-- 导入eureka客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
接口代码:
package cn.zyp;

import java.util.Map;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


/**
 * 用来声明服务名称
 * @author Administrator
 *
 */
@FeignClient("EMAIL")
public interface ISendMail {

    /**
     * @RequestBody用来解析请求体
     * @param map
     * @return
     */
    @PostMapping("/send")
    public String send(@RequestBody Map<String,Object> map);
}
控制层代码:
package cn.zyp;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class MainController {
    @Autowired
    private ISendMail sm;
    @ResponseBody
    @PostMapping("/sendemail")
    public String sendmail(String to,String subject,String text){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("to", to);
        map.put("subject", subject);
        map.put("text", text);
        String res=sm.send(map);
        return res;
    }
}
启动类代码:
package cn.zyp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


/**  
 * @EnableDiscoveryClient 将当前服务注册到Eureka服务器  
 * @EnableEurekaClient继承@EnableDiscoveryClient 作用相同不要纠结  
 *有 @EnableEurekaClient可以不用@EnableDiscoveryClient
 *有@SpringBootApplication可以不用@Configuration
 */  
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}
配置文件:
eureka.client.serviceUrl.defaultZone=http://admin:zyp@localhost:8761/eureka/
spring.application.name=SENDEMAIL
server.port=8888
EMAIL.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.ZoneAvoidanceRule
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值