spring cloud 注册中心

一 .服务端配置

  • 创建 maven项目 在 pom.xml中配置 需要引入spring-cloud-starter-eureka-server 依赖 和 添加 @EnableEurekaServer 注解
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<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.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>

</dependencies>
  • 创建一个application.yml
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${serve
  • 创建一个启动类 启动项目
package cn.et;

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);
    }
}

二. 配置一个发邮件的客户端

  • 创建maven项目 在pom.xml中添加 发邮件的依赖jar包
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<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>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>
  • 创建一个application.properties配置文件 配置访问邮箱的参数
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.163.com
spring.mail.password=密码
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.username=邮箱名称
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=SEND_MAIL
server.port=8082
  • 创建一个controller 发送邮件的业务
package cn.et;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MailController {
    @Autowired
    private JavaMailSender jms;
        @GetMapping("/send")
        public String send(){
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //谁发的
            mailMessage.setFrom("邮箱地址");
            //发给谁
            mailMessage.setTo("邮箱地址");
            //标题
            mailMessage.setSubject("标题");
            //内容
            mailMessage.setText("内容");
            jms.send(mailMessage);
            return "1";
        }
}
  • 创建一个启动类 启动项目
package cn.et;

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);
    }
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_37928350/article/details/78968307
今日推荐